﻿using System.Windows;
using hygge_imaotai.Domain;
using NLog.Config;
using NLog.Targets.Wrappers;
using NLog;
using imaotai.Domain;

namespace hygge_imaotai.UserInterface.UserControl {
    /// <summary>
    /// HomeUserControl.xaml 的交互逻辑
    /// </summary>
    public partial class HomeUserControl : System.Windows.Controls.UserControl {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        public HomeUserControl() {
            InitializeComponent();

            InitUILogging(LogLevel.Info);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HomeUserControl_OnLoaded(object sender, RoutedEventArgs e) {
      
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logLevel"></param>
        private void InitUILogging(LogLevel logLevel) {
            // log layout format
            //var layout = "${message} ${exception:separator=\r\n:format=message,type,method,stackTrace:maxInnerExceptionLevel=10:innerExceptionSeparator=\r\n:innerFormat=message,type,method,stackTrace}";
            //var layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
            var layout = "[${date:format=HH\\:MM\\:ss}][${level:uppercase=true:padding=-5}] ${message}";
            //var layout = "${time:}|${threadid:padding=3}|${level:uppercase=true:padding=-5}|${logger:padding=-15}|${message}|${exception}";
            //var layout = "${time:}|${threadid:padding=3}|${level:uppercase=true:padding=-5}|${message}|${exception}";
            // create rich text box target
            var uiTarget = new WpfRichTextBoxTarget {
                Layout = layout,
                TargetRichTextBox = loggerTextBox,
                UseDefaultRowColoringRules = true,
                AutoScroll = true,
                MaxLines = 250,
            };
            var asyncWrapper = new AsyncTargetWrapper {
                Name = "logEidt",
                WrappedTarget = uiTarget
            };
            var config = LogManager.Configuration ?? new LoggingConfiguration();

            config.AddTarget("UI", uiTarget);
            config.LoggingRules.Add(new LoggingRule("*", logLevel, asyncWrapper));

            LogManager.Configuration = config;
        }
    }
}
